package com.ajun.service.impl;

import com.ajun.common.entity.R;
import com.ajun.entity.Payment;
import com.ajun.mapper.PaymentMapper;
import com.ajun.service.PaymentService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * Payment Service Impl
 *
 * @author AJun
 * @date 2021-07-08
 */
@Service("paymentService")
@Slf4j
public class PaymentServiceImpl implements PaymentService {

    @Resource
    private PaymentMapper paymentMapper;

    @Override
    public R<Boolean> createPayment(Payment payment) {
        int count = paymentMapper.insert(payment);
        log.info("PaymentMapper.insert count: {}", count);
        if (count > 0) {
            log.info("PaymentService.createPayment success, payment: {}", JSON.toJSONString(payment));
            return R.ok(true, "创建成功");
        }
        log.error("PaymentService.createPayment failure, payment: {}", JSON.toJSONString(payment));
        return R.fail(false, "创建失败");
    }

    @Override
    public R<Payment> getPayment(Long id) {
        Payment payment = paymentMapper.selectById(id);
        log.info("PaymentService.getPayment success, payment: {}", JSON.toJSONString(payment));
        if (Objects.isNull(payment)) {
            return R.ok(null, "暂无结果");
        }
        return R.ok(payment, "查询成功");
    }
}
